<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-input
        class="a"
        placeholder="请输入内容"
        @clear="getData"
        v-model="form.query"
        clearable
      >
        <template slot="append"
          ><i @click="getData" class="el-icon-search"></i
        ></template>
      </el-input>
      <el-button type="primary" @click="addPage">添加商品</el-button>

      <el-table
        :data="arr"
        border
        :header-cell-style="{ background: '#EEF1F6', color: '#606266' }"
      >
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80">
        </el-table-column>
        <el-table-column prop="upd_time" label="创建时间" width="180">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="update(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.pagenum"
        :page-sizes="[5, 10, 50, 100]"
        :page-size="form.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      arr: [],
      total: 0,
    };
  },
  methods: {
    async del(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
          var res = await this.$http.delete(`goods/${row.goods_id}`);
          this.hint(res)
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    update(row) {
      this.$router.push(`/goods/update/${row.goods_id}`);
    },
    addPage() {
      this.$router.push("/goods/add");
    },
    handleSizeChange(size) {
      this.form.pagesize = size;
      this.getData();
    },
    handleCurrentChange(num) {
      this.form.pagenum = num;
      this.getData();
    },
    async getData() {
      var { data: res } = await this.$http.get("goods", {
        params: this.form,
      });

      this.arr = res.data.goods;
      this.total = res.data.total;
    },
    //提示 成功或失败 的信息
    hint(res) {
      if (res.data.meta.status == 200 || res.data.meta.status == 201) {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "success",
        });
        this.getData()
      } else {
        this.$message({
          showClose: true,
          message: res.data.meta.msg,
          type: "error",
        });
      }
    },
  },

  created() {
    this.getData();
  },
};
</script>

<style>
.el-table {
  margin-top: 20px;
  font-size: 12px;
}
.el-button {
  margin-left: 20px !important;
}
.el-pagination {
  margin-top: 20px;
}
</style>